package api
{
	import SEApp.dialogs.se_alert;
	
	import api.com.odnoklassniki.Odnoklassniki;
	import api.com.odnoklassniki.sdk.friends.Friends;
	import api.com.odnoklassniki.sdk.photos.Photos;
	
	import api_servers.api_externalserver;
	
	import flash.display.DisplayObjectContainer;

	public class clt_okserver extends api_externalserver {
		public function clt_okserver(a_sroot: DisplayObjectContainer, a_vars: Object) {
			vars = a_vars;
			super(a_sroot, vars.api_server, vars.logged_user_id, vars.api_id, vars.application_key, 0);
		}
		
		override public function isAdmin(): Boolean {
			return viewer_id == '351762715688';
		}
		
		override public function convertVars():Object {
			return {
				preview_url		: {name:'src_small'},
				standard_url	: {name:'src_big'},
				gender			: {name:'sex', cnv: function(val: String): uint {
							return (val=='male')?0:1; // Приведение к стандарту моего мира: 0- мужчина, 1-женщина
						}
				},
				pic_2			: {name: 'pic'},
				pic_3			: {name: 'pic_small'},
				pic_4			: {name: 'pic_big'},
				name			: {name: 'nick'},
				url_profile		: {name: 'link'}
			}
		}
		
		protected function cHandle(_handle: Function, property: String): Function {
			return function(data: Object): void {
				if (!clt.checkErrorData(data)) {
					argConvert(data[property])
					_handle(data[property]);
				}
			}
		}
		
		protected function get clt(): clothing_ok {
			return sroot as clothing_ok;
		}
		
		public function getAlbums(uid: String, handler_: Function, onError: Function=null): void {
			Photos.getAlbums(cHandle(handler_, 'albums'), uid);
		}
		
		public function getPhotos(uid: String, aid: String, handler_: Function): void {
			Photos.getUserAlbumPhotos(aid, cHandle(handler_, 'photos'), uid, '', '', 100);
		}
		
	    public function getAllFriends(uid: String, handler_:*, queryID:String='getAllFriends'): void {
	    	if (_cache[queryID]) handler_(_cache[queryID]);
	    	else {
				Friends.get(function(a_data: Array): void {
					getProfiles(a_data.join(','), handler_, '', queryID);
				}, uid);
	    	}
	    }
		
		public function getAppUsers(handler_: Function): void {
			Friends.getAppUsers(function(a_data: Object): void {
				handler_(a_data.uids);
//				getProfiles(a_data.uids.join(','), handler_, '', 'getAppUsers');
			});
		}

	    override public function getProfiles(a_uids: String, handler_:*, a_fields: String='', queryID:String=''): void {
			if (queryID && _cache[queryID]) handler_(_cache[queryID]);
			else {
				a_fields = (a_fields)?a_fields:'uid,first_name,last_name,name,gender,birthday,age,url_profile,online,pic_2,pic_3,pic_4';
				
				var a_result	: Array = new Array();
				var qcount		: int 	= 0;
				var a_data		: Array = a_uids.split(',');
				
				function doResponse(a_data: Array): void {
					a_result = a_result.concat(a_data);
					qcount--;
					if (qcount == 0) {
						handler_(a_result);
						_cache[queryID] = a_result;
					}
				}
				
				while (a_data.length > 0) {
					qcount++;
					Odnoklassniki.callRestApi("users.getInfo", function(data: Object): void {
						if (!clt.checkErrorData(data)) {
							argConvert(data);
							doResponse(data);
						}
					}, 
						Odnoklassniki.getSendObject({
							uids			:a_data.slice(0, 100).join(','), 
							fields			:a_fields, 
							emptyPictures	:true
						})
					);
					a_data.splice(0, 100);
				}
				
			}

	    }
	}
}